package com.afd.ozblip.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.afd.ozblip.activity.R;
import com.afd.ozblip.entity.UserMsgEntity;
import com.afd.ozblip.helper.UserMsgVH;
import com.afd.ozblip.image.LoadImage;
import com.afd.ozblip.util.CollectionUtil;
import com.afd.ozblip.util.Constants;
import com.afd.ozblip.util.StringUtil;

public class UserMsgListAdapter extends BaseAdapter {

	public List<UserMsgEntity> dataList;
	private LayoutInflater inflater;
	private Context context;
	private LoadImage loadImage;

	public UserMsgListAdapter(Context paramContext) {
		context = paramContext;
		this.dataList = new ArrayList<UserMsgEntity>();
		this.inflater = LayoutInflater.from(context);
		this.loadImage = new LoadImage(context);
	}

	/**
	 * 获取列表条数
	 */
	@Override
	public int getCount() {
		if (CollectionUtil.UnNullAndZero(this.dataList)) {
			return this.dataList.size();
		} else {
			return 0;
		}

	}

	@Override
	public Object getItem(int paramInt) {
		if (CollectionUtil.UnNullAndZero(this.dataList)) {
			return this.dataList.get(paramInt);
		} else {
			return null;
		}
	}

	@Override
	public long getItemId(int paramInt) {
		return paramInt;
	}

	@Override
	public View getView(int position, View paramView, ViewGroup paramViewGroup) {
		try {
			UserMsgVH msgVH = null;
			if (paramView == null) {
				msgVH = new UserMsgVH();
				paramView = this.inflater.inflate(R.layout.user_msg_item, null);

				msgVH.userImageIv = (ImageView) paramView
						.findViewById(R.id.user_msg_user_head_img_iv);
				msgVH.userNameTv = (TextView) paramView
						.findViewById(R.id.user_msg_user_nick_name_tv);
				msgVH.msgContentTv = (TextView) paramView
						.findViewById(R.id.user_msg_content_tv);
				msgVH.msgTimeTv = (TextView) paramView
						.findViewById(R.id.user_msg_time_tv);
				paramView.setTag(msgVH);
			} else {
				msgVH = (UserMsgVH) paramView.getTag();
			}

			UserMsgEntity msg = this.dataList.get(position);
			if (msg != null) {
				// 好友头像
				String bName = msg.getUserImgUrl();
				if (!StringUtil.isNullOrEmpty(bName)) {
					loadImage.addTask(Constants.WEB_PATH + bName,
							msgVH.userImageIv);
				} else {
					msgVH.userImageIv
							.setImageResource(R.drawable.default_head_pic);
				}
				// 好友昵称
				String nickName = msg.getUserName();
				if (!StringUtil.isNullOrEmpty(nickName)) {
					msgVH.userNameTv.setText(nickName);
				}
				// 消息内容
				String msgContent = msg.getMessageDetails();
				if (!StringUtil.isNullOrEmpty(msgContent)) {
					msgVH.msgContentTv.setText(msgContent);
				}
				// 消息时间
				String bClass = msg.getMessageTime();
				if (!StringUtil.isNullOrEmpty(bClass)) {
					msgVH.msgTimeTv.setText(bClass);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return paramView;
	}

	/**
	 * 添加多项
	 */
	public void addItems(List<UserMsgEntity> commentList) {
		this.dataList.addAll(commentList);
	}

	/**
	 * 清除所有项
	 */
	public void removeAllItem() {
		this.dataList.clear();
	}
}
